package maa.dao;

import maa.action.ActionException;
import maa.entity.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LogDAO implements DAOCommand {
    private static final Logger logger = LoggerFactory.getLogger(LogDAO.class);
    private static final String FIND = "SELECT * FROM Log.LOGS limit ? offset ?";
    private static final String ROW_COUNT = "select count(*) from log.logs";
    private Connection connection;

    public LogDAO(Connection connection){
        this.connection = connection;
    }

    @Override
    public Object execute(DAOManager daoManager) throws ActionException {
        return new Object();
    }

    public int amount() throws DAOException {
        int i = 0;
        PreparedStatement preparedStatement = null;
        if (connection != null) {
            try {
                preparedStatement = connection.prepareStatement(ROW_COUNT);
                ResultSet resultSet = preparedStatement.executeQuery();
                resultSet.next();
                i = resultSet.getInt(1);
            } catch (SQLException e) {
                logger.error("Could not find logs. " + e.getMessage());
                throw new DAOException(e.getMessage());
            } finally {
                if (preparedStatement != null) {
                    close(preparedStatement);
                }
            }
        } else {
            throw new DAOException("No connection available");
        }
        return i;
    }

    @SuppressWarnings("JpaQueryApiInspection")
    public List<Log> find(int x, int y) throws DAOException {
        List<Log> logs = new ArrayList<>();
        if (connection != null) {
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(FIND);
                preparedStatement.setInt(1, x);
                preparedStatement.setInt(2, y);
                ResultSet resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    Log log = createEntity(resultSet);
                    logs.add(log);
                }
            } catch (SQLException e) {
                logger.error("Could not find logs. " + e.getMessage());
                throw new DAOException(e.getMessage());
            } finally {
                if (preparedStatement != null) {
                    close(preparedStatement);
                }
            }
        } else {
            throw new DAOException("No connection available");
        }
        return logs;
    }

    private Log createEntity(ResultSet set) {
        Log log = new Log();
        try {
            log.setDate(set.getTimestamp(1));
            log.setLogger(set.getString(2));
            log.setLevel(set.getString(3));
            log.setMessage(set.getString(4));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return log;
    }

    public void close(PreparedStatement statement) {
        try {
            statement.close();
        } catch (SQLException e) {
            logger.error(e.getMessage());
        }
    }
}
